package cn.com.doone.common.uc.utils;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;

public class SmsUtils {

	/**
	 * 发送账号密码短信
	 * 
	 * @param smsUrl
	 *            短信接口
	 * @param smsAppKey
	 * @param smsSecret
	 * @param smsExtend
	 * @param smsSignName
	 *            阿里认证签名
	 * @param smsParamString
	 *            短信内容,json格式
	 * @param recNum
	 *            接收手机号码
	 * @param smsTemplateCode
	 */
	public static void smsSendMessage(Map<String, String> paramsMap) throws Exception {
		if (StringUtils.isNull(paramsMap.get("smsUrl")) || StringUtils.isNull(paramsMap.get("smsAppKey"))
				|| StringUtils.isNull(paramsMap.get("smsTemplateCode"))
				|| StringUtils.isNull(paramsMap.get("smsSecret")) || StringUtils.isNull(paramsMap.get("smsParamString"))
				|| StringUtils.isNull(paramsMap.get("recNum"))) {
			throw new Exception("参数不完整");
		}

		// 阿里大于短信接口
		TaobaoClient client = new DefaultTaobaoClient(paramsMap.get("smsUrl"), paramsMap.get("smsAppKey"),
				paramsMap.get("smsSecret"));

		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
		req.setExtend("123456");
		req.setSmsType("normal"); // normal类型为短信通知
		req.setSmsFreeSignName("新东网"); // 阿里大于认证的签名
		req.setSmsParamString(paramsMap.get("smsParamString"));
		req.setRecNum(paramsMap.get("recNum")); // 手机号
		req.setSmsTemplateCode(paramsMap.get("smsTemplateCode"));

		AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
		System.out.println(rsp.getBody());
	}

	public static void main(String[] args) throws Exception {
		Map<String, String> smsMap = new HashMap<String, String>();
		smsMap.put("smsUrl", "http://gw.api.taobao.com/router/rest");
		smsMap.put("smsAppKey", "23449624");
		smsMap.put("smsSecret", "bd4ba9bf9b030b62af74af4dd49a4529");
		smsMap.put("smsExtend", "123456");
		smsMap.put("smsSignName", "新东网");
		smsMap.put("smsTemplateCode", "SMS_14771077");
		JSONObject smsParamsMap = new JSONObject();
		smsParamsMap.put("useraccount", "chenjf");
		smsParamsMap.put("password", "123123");
		smsMap.put("smsParamString", JSON.toJSONString(smsParamsMap));
		smsMap.put("recNum", "17759188225");
		SmsUtils.smsSendMessage(smsMap);
	}
}
